package com.scau.clothes.api.oss;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.domain.FileInfo;
import com.scau.clothes.domain.Oss;
import com.scau.clothes.service.FileService;
import com.scau.clothes.service.IOssService;
import com.scau.clothes.core.core.entity.AjaxResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * (Oss)表控制层
 */
@RestController
@RequestMapping("oss")
public class OssController {
    /**
     * 服务对象
     */
    @Autowired
    private IOssService ossService;

    @Autowired
    private FileService fileService;

    /**
     * 查询数据列表
     *
     * @param oss 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public AjaxResult list(Oss oss) {
        return AjaxResult.success(this.ossService.selectList(oss));
    }

    /**
     * 分页查询数据列表
     *
     * @param oss 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Oss oss) {
        PageHelper.startPage(pageNum,pageSize);
        List<Oss> list = this.ossService.selectList(oss);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.ossService.selectById(id));
    }

    /**
     * 新增数据
     *
     * @param oss 实体
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody Oss oss) {
        return AjaxResult.success(this.ossService.insert(oss));
    }

    /**
     * 编辑数据
     *
     * @param oss 实体
     * @return 编辑结果
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Oss oss) {
        return AjaxResult.success(this.ossService.update(oss));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        return AjaxResult.success(this.ossService.deleteById(id));
    }

    /**
     * 以流的形式上传图片
     *
     * @param file
     * @return 返回访问路径
     */
    @PostMapping("upload")
    public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file, HttpSession session)  {
        FileInfo fileInfo = fileService.uploadFile(file);
        System.out.println("测试是否能接收前端上传文件：" + file.getOriginalFilename());
        fileService.getUrlByName(fileInfo.getFileName());
        System.out.println("获取url：" + fileService.getUrlByName(fileInfo.getFileName()));
        String url=fileService.getUrlByName(fileInfo.getFileName());
        // 一个临时存储URL的方法
        session.setAttribute("uploadedFileUrl", url);
        return AjaxResult.success("上传成功",url);
    }
}

